package com.hut.oa.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hut.oa.model.bean.ScheduleManage;
import com.hut.oa.model.bean.User;
import com.hut.oa.model.dao.scedule.SceduleDao;
import com.hut.oa.model.dao.user.UserDao;
import com.hut.oa.model.mapper.CalendarMapper;

@Controller
@RequestMapping("/calendar")
public class CalendarController {

	@Autowired
	SceduleDao sd;

	@Autowired
	UserDao ud;
	
	@Autowired
	CalendarMapper cm;
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 日历
	 * 
	 * @return
	 */
	@RequestMapping
	public String calendar() {
		return "calendar/calendar";
	}

	/**
	 * 全部日历
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/allcalendar")
	public String allcalendar(String key,Model model, @PageableDefault(value = 10, sort = { "smId" }) Pageable pageable,HttpSession session) {
		User user = (User) session.getAttribute("userinfo");
		Page<ScheduleManage> sm = sd.findBySmSendUserId(user.getId(), pageable);
		if (key!=null) {
			List<Map<String, Object>> we=cm.selectcid(user.getId());
			List<Long> c=new ArrayList<>();
			for (Map<String, Object> map : we) {
				c.add(Long.parseLong(map.get("scedule_manage_sm_id")+""));
			}
			sm = sd.findbyuserandcoruid(user.getId(), key,c, pageable);
		}
		model.addAttribute("sm", sm);
		return "calendar/allcalendar";
	}

	/**
	 * 新增界面
	 * 
	 * @return
	 */
	@RequestMapping("/addcalendar")
	public String addcalendar(HttpServletRequest request,HttpSession session) {
		SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd 12:00:00");
		Date date = new Date();
		date.setDate(date.getDate() + 1);
		request.setAttribute("start", dateFormater.format(new Date()));
		request.setAttribute("end", dateFormater.format(date));
		Iterable<User> user=ud.findAll();
		request.setAttribute("users", user);
		return "calendar/addcalendar";
	}

	/**
	 * 查看界面
	 * 
	 * @return
	 */
	@RequestMapping("/checkcalendar")
	public String checkcalendar(HttpServletRequest request) {
		String id = request.getParameter("id");
		ScheduleManage sm = sd.findBySmId(Long.parseLong(id));
		request.setAttribute("sm", sm);
		request.setAttribute("sendUser", ud.findOne(Long.parseLong(sm.getSmSendUserId() + "")));
		return "calendar/checkcalendar";
	}

	/**
	 * 查询全部日历
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/findall", method = RequestMethod.POST)
	public Iterable<ScheduleManage> findall(HttpSession session) {
		User user = (User) session.getAttribute("userinfo");
		List<Map<String, Object>> we=cm.selectcid(user.getId());
		List<Long> c=new ArrayList<>();
		for (Map<String, Object> map : we) {
			c.add(Long.parseLong(map.get("scedule_manage_sm_id")+""));
		}
		List<ScheduleManage> sm=sd.findByUserId(user.getId());
		List<ScheduleManage> sm2=sd.findbyids(c);
		List<ScheduleManage> sm3=sd.findByUserId(user.getId());
		if (sm!=null) {
			for (ScheduleManage sche : sm) {
				if (sm2!=null) {
					for (ScheduleManage sche2 : sm2) {
						if (sche2.getSmId()!=sche.getSmId()) {
							sm3.add(sche2);
						}
					}
				}else {
					break;
				}
			}
		}else {
			sm3=sm2;
		}
		return sm3;
	}

	/**
	 * 日历
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/tixing")
	public Boolean tixing(Long id, Integer remind) {
		ScheduleManage sm = sd.findBySmId(id);
		sm.setSmRemind(remind);
		if (sd.save(sm) != null) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 获取原有新增信息
	 * 
	 * @return
	 */
	@RequestMapping("/addcalendars")
	public String addcalendars(HttpServletRequest request) {
		String title = request.getParameter("title");
		String start = request.getParameter("start");
		String end = request.getParameter("end");
		request.setAttribute("title", title);
		request.setAttribute("start", start);
		request.setAttribute("end", end);
		return "calendar/addcalendar";
	}

	/**
	 * 添加日程
	 * 
	 * @return
	 * @throws ParseException
	 */
	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Boolean add(HttpServletRequest request,HttpSession session) throws ParseException {
		User user = (User) session.getAttribute("userinfo");
		String title = request.getParameter("title");
		String start = request.getParameter("start");
		String end = request.getParameter("end");
		String statu = request.getParameter("statu");
		String content = request.getParameter("content");
		String id = request.getParameter("id");
		String us=request.getParameter("users");
		String[] users=us.split(",");
		// 发布者id
		// String userId = request.getParameter("userId");
		String remind = request.getParameter("remind");
		ScheduleManage sm = new ScheduleManage();
		sm.setSmTitle(title);
		sm.setSmStartTime(sdf.parse(start));
		sm.setSmEndTime(sdf.parse(end));
		sm.setSmSendUserId(user.getId());
		sm.setSmStatus(Long.parseLong(statu));
		if (content != null) {
			sm.setSmContent(content);
		}
		if (id != "0" && id != null) {
			sm.setSmId(Long.parseLong(id));
		}
		if (remind != null) {
			sm.setSmRemind(Integer.parseInt(remind));
		}
		if (users!=null) {
			List<User> userlist = new ArrayList<>();
			for (String string : users) {
				userlist.add(ud.findById(Long.parseLong(string)));
			}
			sm.setUsers(userlist);
		}
		if (sd.save(sm) != null) {
			return true;
		} else {
			return false;
		}

	}

	/**
	 * 通过id查询日历
	 * 
	 * @return
	 * @throws ParseException
	 */
	@ResponseBody
	@RequestMapping(value = "/findbyid", method = RequestMethod.POST)
	public ScheduleManage findabyid(Long id) throws ParseException {
		ScheduleManage sm = sd.findBySmId(id);
		return sm;
	}

	/**
	 * 通过id删除日历
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public void delete(HttpServletRequest request) {
		String id = request.getParameter("id");
		sd.delete(Long.parseLong(id));
	}

	/**
	 * 编辑日历
	 * 
	 * @return
	 */
	@RequestMapping(value = "/op")
	public String op(HttpServletRequest request) {
		String id = request.getParameter("id");
		ScheduleManage sm = sd.findBySmId(Long.parseLong(id));
		request.setAttribute("sm", sm);
		Iterable<User> user=ud.findAll();
		request.setAttribute("users", user);
		request.setAttribute("id", id);
		return "calendar/addcalendar";
	}
}
